<?php
include_once('./config/init.php');

//判断是否登录
AuthLogin();

//如果有传递分页参数 就拿 如果没有就 默认 第一页
$page = isset($_GET['page']) ? $_GET['page'] : 1;

//每页显示多少条
$limit = 5;

//查询当前的总条数出来
$sql = "SELECT count(*) AS c FROM {$pre_}person";
$count = find($sql);
$count = isset($count['c']) ? $count['c'] : 0;

//中间显示多少个页码
$size = 5;

//调用分页函数
$PageStr = page($page, $count, $limit, $size);

//偏移量 查询起始位置
// LIMIT 起始位置0，每页显示多少条
// LIMIT 0,5;
// LIMIT 5,5;
$start = ($page - 1) * $limit;

//查询数据
$sql = "SELECT per.*, dep.name AS depName, job.name AS jobName FROM {$pre_}person AS per LEFT JOIN {$pre_}department AS dep ON per.depid = dep.id LEFT JOIN {$pre_}job AS job ON per.jobid = job.id LIMIT $start,$limit";

$list = all($sql);

//删除员工数据
if ($_POST) {
    //接收id
    $ids = isset($_POST['ids']) ? trim($_POST['ids']) : 0;
    //批量删除的时候要把员工的头像图片也要删除

    //查询
    $sql = "SELECT avatar FROM {$pre_}person WHERE id IN($ids)";

    $list = all($sql);

    //可以提取数组中指定的下标元素出来，放到一个新数组里面
    $avatar = array_column($list, 'avatar');

    //将空值过滤掉
    $avatar = array_filter($avatar);

    //将数组中所有的选项都唯一 去除重复选项
    $avatar = array_unique($avatar);

    //如果没有图片就直接删除数据库记录
    $affect = delete("person", "id IN($ids)");

    if($affect){
        //如果数组不为空才去删除图片
        if(!empty($avatar)){
            foreach($avatar as $item){
                //删除文件
                @is_file($item) && @unlink($item);
            }
        }

        ShowMsg('员工删除成功','person_list.php');
        exit;
    }else{
        ShowMsg('员工删除失败');
        exit;
    }
}



?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('meta.php'); ?>
</head>

<body>
    <?php include_once('header.php'); ?>

    <?php include_once('menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">员工管理</h1>
        </div>

        <ul class="breadcrumb">
            <li>
                <a href="index.php">控制台</a>
                <span class="divider">/</span>
            </li>
            <li class="active">员工列表</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='person_add.php'">
                        <i class="icon-plus"></i>添加新员工
                    </button>
                </div>

                <!-- 数据列表 -->
                <div class="well">

                    <table class="table datalist">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="all" /></th>
                                <th>员工昵称</th>
                                <th>性别</th>
                                <th>手机号</th>
                                <th>入职时间</th>
                                <th>所属部门</th>
                                <th>所属职位</th>
                                <th style="width: 100px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($list as $item) { ?>
                                <tr>
                                    <td>
                                        <!-- 以数组的方式进行数据传递 -->
                                        <input type="checkbox" name="list" value="<?php echo $item['id'] ?>" />
                                    </td>
                                    <td><?php echo $item['nickname']; ?></td>
                                    <td>
                                        <?php if ($item['sex'] == 0) { ?>
                                            保密
                                        <?php } else if ($item['sex'] == 1) { ?>
                                            男
                                        <?php } else if ($item['sex'] == 2) { ?>
                                            女
                                        <?php } else { ?>
                                            未知数据
                                        <?php } ?>
                                    </td>
                                    <td><?php echo $item['phone']; ?></td>
                                    <td><?php echo date("Y-m-d", $item['createtime']); ?></td>
                                    <td><?php echo $item['depName']; ?></td>
                                    <td><?php echo $item['jobName']; ?></td>
                                    <td>
                                        <a class="btn btn-primary" href="person_edit.php?id=<?php echo $item['id']; ?>">
                                            <i class="icon-pencil"></i>
                                        </a>
                                        <a onclick="delone(<?php echo $item['id']; ?>)" class="btn btn-danger" href="#Modal" role="button" data-toggle="modal">
                                            <i class="icon-remove"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php } ?>
                            <tr>
                                <td class="delete" colspan="10">
                                    <a onclick="delall()" href="#Modal" role="button" data-toggle="modal" class="btn btn-danger">批量删除</a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <div class="pagination">
                    <?php echo $PageStr; ?>
                </div>

                <!-- 模态框 -->
                <form method="post">
                    <div class="modal small hide fade" id="Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">删除对话框</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除员工?</p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                            <button class="btn btn-danger" type="submit">确认</button>
                        </div>
                        <!-- 隐藏域 看不见的表单元素 -->
                        <input type="hidden" name="ids" value="" />
                    </div>
                </form>

                <footer>
                    <hr>
                    <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                </footer>
            </div>
        </div>
    </div>
</body>

</html>
<script>
    $("#all").click(function() {
        $("input[name=list]").prop('checked', $(this).prop('checked'))
    })

    //单条删除
    function delone(perid) {
        //将id写入到提交表单中的隐藏域选项上
        $("input[name=ids]").val(perid)
    }

    //多条删除
    function delall() {
        var ids = []

        $("input[name=list]").each(function(index, el) {
            //获取元素的选中属性
            var checked = $(this).prop('checked')

            //如果选中就将id 插入到数组中
            if (checked) {
                ids.push($(this).val())
            }
        })

        //数组 转换成 字符串  join
        ids = ids.join(',')
        $("input[name=ids]").val(ids)
    }
</script>